package com.bestcem.xm.user.grpc.service;

import com.bestcem.bp.xm.user.grpc.v1.services.ListUserScenariosResponse;
import com.bestcem.bp.xm.user.grpc.v1.services.UserScenarioServiceGrpc;
import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.user.entity.mysql.UserScenario;
import com.bestcem.xm.user.grpc.service.converter.UserScenarioConverter;
import com.bestcem.xm.user.service.UserScenarioService;
import com.google.protobuf.Empty;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户使用场景grpc
 *
 * @author YiFang <yifang.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2022/7/14 18:17
 */
@Slf4j
@GrpcService
public class UserScenarioGrpcService extends UserScenarioServiceGrpc.UserScenarioServiceImplBase {


    @Resource
    UserScenarioService userScenarioService;

    /**
     * 更新User
     *
     * @param responseObserver 响应结果
     * @return void
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/3/8 18:15
     */
    @Override
    public void listUserScenarios(Empty request, StreamObserver<ListUserScenariosResponse> responseObserver) {

        ListUserScenariosResponse.Builder builder = ListUserScenariosResponse.newBuilder();
        ServiceResult<List<UserScenario>> listServiceResult = userScenarioService.listScenarios();
        if (listServiceResult.isSuccess()) {
            List<UserScenario> datas = listServiceResult.getData();
            builder.addAllScenarios(UserScenarioConverter.entityListToGrpcEntityList(datas));
            // 查询成功
            builder.setStatus(RpcStatusResult.success());
        } else {
            builder.setStatus(RpcStatusResult.error(listServiceResult.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }
}
